const router = require('express').Router({});
const {check, validationResult} = require('express-validator'); //数据验证
const studentControllers = require("../../controllers/studentControllers");
/* GET */
router.get(
    '/',
    [
        check('pageNo').notEmpty().withMessage('页码不能为空'),
        check('pageSize').notEmpty().withMessage('条数不能为空')
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }
        let data = await studentControllers
            .selectList(
                req.query.pageNo,
                req.query.pageSize,
                req.query.sname,
                req.query.cname,
                req.query.major
            );

        res.sendResult(data, 200, "请求成功");

    }
);

let dateTime = /([1-2][0-9][0-9][0-9]-[0-1]?[0-9]-[0-3]?[0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9])|([1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]-[0-3]{0,1}[0-9])/;
router.post(
    '/',
    [
        check('student_class_id').notEmpty().withMessage('student_class_id参数不能为空'),
        check('student_number').notEmpty().withMessage('student_number参数不能为空')
            .custom(student_number => {
                if (!(/1\d{10}/.test(student_number))) {
                    throw new Error('student_number不是合法手机号')
                }
                return true
            }),
        check('student_name').notEmpty().withMessage('student_name参数不能为空'),
        check('student_email').notEmpty().withMessage('student_email参数不能为空')
            .isEmail().withMessage('student_email不是合法邮箱号'),
        check('student_birthday').notEmpty().withMessage('student_birthday参数不能为空')
            .custom(student_birthday => {
                if (!dateTime.test(student_birthday)) {
                    throw new Error('student_birthday不是合法日期 yyyy-MM-dd')
                }
                return true
            }),
        check('student_sex').notEmpty().withMessage('student_sex参数不能为空')
            .isIn(["0", "1"]).withMessage('student_sex性别参数错误'),
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }

        let data1 = await studentControllers.selectByNumber(req.body.student_number);
        console.log(data1);

        if (data1) {
            res.sendResult(null, 400, "添加失败,学生手机号已存在");
            return;
        }
        await studentControllers.insert(req.body)
        res.sendResult({}, 201, "添加成功,默认密码是 123456");
    }
);

router.put(
    '/',
    [
        check('student_id').notEmpty().withMessage('student_id参数不能为空'),
        check('student_class_id').notEmpty().withMessage('student_class_id参数不能为空'),
        check('student_number').notEmpty().withMessage('student_number参数不能为空')
            .custom(student_number => {
                if (!(/1\d{10}/.test(student_number))) {
                    throw new Error('student_number不是合法手机号')
                }
                return true
            }),
        check('student_name').notEmpty().withMessage('student_name参数不能为空'),
        check('student_email').notEmpty().withMessage('student_email参数不能为空')
            .isEmail().withMessage('student_email不是合法邮箱号'),
        check('student_birthday').notEmpty().withMessage('student_birthday参数不能为空')
            .custom(student_birthday => {
                if (!dateTime.test(student_birthday)) {
                    throw new Error('student_birthday不是合法日期 yyyy-MM-dd')
                }
                return true
            }),
        check('student_sex').notEmpty().withMessage('student_sex参数不能为空')
            .isIn(["0", "1"]).withMessage('student_sex性别参数错误'),
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }

        let data1 = await studentControllers.selectByNumber(req.body.student_number, req.body.student_id);
        if (data1) {
            res.sendResult(null, 400, "更新失败,学生手机号已存在");
            return;
        }

        let flag = await studentControllers.updateByDid(req.body, req.body.student_id);
        res.sendResult({}, 202, flag ? "更新成功" : "数据已更新");
    }
);

router.delete(
    '/',
    [
        check('student_id').notEmpty().withMessage('student_id参数不能为空'),
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }

        let data = await studentControllers.selectInfoByStudentId(req.query.student_id);

        if (data.length == 0) {
            res.sendResult(null, 400, "删除失败,student_id不存在");
            return;
        }

        let flag = await studentControllers.deleteByDid(req.query.student_id);
        res.sendResult({}, 203, flag ? "删除成功" : "数据已删除");
    }
);


module.exports = router;

